// pages/payfees/payfees.js
import websocket from '../../utils/websocket'
let request = require("../../utils/request.js"); //封装的request请求

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: 0,
    websocketUrl: '',
    payFessList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight,
      websocketUrl: 'ws://192.168.92.47:9468/webSocket/' + wx.getStorageSync('token')
    })
  },
  getPayFees() {
    let that = this
    let url = ''
    request.http(url,'GET')
      .then(res => {
        if (res.data.code == 200) {
          that.setData({
            payFessList: res.data.data
          })
        }
      })
  },
  getsocket() {
    let _this = this;
    wx.showLoading({
      title: '正在付款',
    })
    // 创建websocket对象
    this.websocket = new websocket({
      // true代表启用心跳检测和断线重连
      heartCheck: true,
      isReconnection: true
    });
    // 建立连接
    this.linkWebsocket();
    // 监听websocket状态
    this.websocket.onSocketClosed({
      url: this.data.websocketUrl,
      success(res) { console.log(res) },
      fail(err) { console.log(err) }
    })
    // 监听网络变化
    this.websocket.onNetworkChange({
      url: this.data.websocketUrl,
      success(res) { console.log(res) },
      fail(err) { console.log(err) }
    })
    // 监听服务器返回
    this.websocket.onReceivedMsg(result => {
      if (result.data == 'onOpenSuccess') {
        wx.hideLoading()
        request.http('/order/orderInfo/test', 'POST')
          .then(res => {
            console.log(res);
            if (res.data.code == 200) {
              _this.websocket.closeWebSocket();
              wx.redirectTo({
                url: '/pages/orderRecord/orderRecord',
              })
            }
          })
      }
      if (result.data == 'ping') {

      } else {
        console.log('app.js收到服务器内容：' + result.data);
      }
      // 要进行的操作
    })
  },
  linkWebsocket() {
    // 建立连接
    this.websocket.initWebSocket({
      url: this.data.websocketUrl,
      success(res) {
        console.log(res)
      },
      fail(err) { console.log(err) }
    })
  }
})